<template>
  <div class="respond-container">
    <el-card v-if="survey" class="survey-card">
      <h2 class="survey-title">{{ survey.title }}</h2>
      <p v-if="survey.description" class="survey-description">
        {{ survey.description }}
      </p>
      <el-divider />

      <el-form ref="formRef" :model="answers" label-position="top">
        <div
            v-for="(question, index) in survey.questions"
            :key="question.id"
            class="question-item"
        >
          <h3 class="question-title">
            {{ index + 1 }}. {{ question.title }}
            <span v-if="question.required" class="required-mark">*</span>
          </h3>

          <AnswerInput
              :question="question"
              v-model="answers[question.id]"
              class="answer-input"
          />
        </div>

        <div class="submit-section">
          <el-button
              type="primary"
              size="large"
              @click="submitSurvey"
              class="submit-btn"
          >
            提交问卷
          </el-button>
        </div>
      </el-form>
    </el-card>

    <el-empty v-else description="问卷不存在" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import AnswerInput from '@/components/survey/AnswerInput.vue'
import { useSurveyStore } from '@/store/modules/survey.store'

const route = useRoute()
const router = useRouter()
const surveyStore = useSurveyStore()

const survey = ref<any>(null)
const answers = ref<Record<string, any>>({})
const formRef = ref<any>(null)

onMounted(() => {
  const surveyId = route.params.id as string
  const found = surveyStore.surveys.find(s => s.id === surveyId)

  if (found) {
    survey.value = found
    surveyStore.currentSurvey = found
  } else {
    ElMessage.error('问卷不存在')
    router.push({ name: 'home' })
  }
})

const submitSurvey = () => {
  // 检查必填项
  if (survey.value) {
    const missingRequired = survey.value.questions.some((q: any) =>
        q.required && !answers.value[q.id]
    )

    if (missingRequired) {
      ElMessage.error('请完成所有必填问题')
      return
    }
  }

  surveyStore.submitSurvey(answers.value)
  ElMessage.success('问卷提交成功！')
  router.push({ name: 'home' })
}
</script>

<style scoped>
.respond-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
}

.survey-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.survey-title {
  text-align: center;
  margin-bottom: 10px;
  color: #303133;
}

.survey-description {
  text-align: center;
  color: #606266;
  margin-bottom: 20px;
}

.question-item {
  margin-bottom: 30px;
  padding: 20px;
  background: #f9f9f9;
  border-radius: 6px;
}

.question-title {
  margin-bottom: 15px;
  color: #303133;
}

.required-mark {
  color: #f56c6c;
  margin-left: 5px;
}

.submit-section {
  margin-top: 40px;
  text-align: center;
}

.submit-btn {
  width: 200px;
  height: 45px;
  font-size: 16px;
}
</style>